﻿using System;
using System.Collections.Generic;

namespace ProblemsSet
{
    public class Problem_12 : BaseProblem
    {
        public override object GetResult()
        {
            const int maxFactors = 500;
            long cur = 1;
            while (true)
            {
                if (GetCountFactors(cur * (cur + 1) / 2) > maxFactors) return (cur*(cur + 1)/2);
                cur++;
            }
            
        }

        public static int GetCountFactors(long value)
        {
            var res = 0;
            var mid = Math.Sqrt(value);
            for (var i = 2; i < mid; i++)
            {
                if (value % i == 0) res++;
            }
            res = 2*res + 2;
            if (mid == Math.Truncate(mid)) res++;
            return res;
        }

        public override string Problem
        {
            get
            {
                return @"The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

Let us list the factors of the first seven triangle numbers:

 1: 1
 3: 1,3
 6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred divisors?";
            }
        }

        public override bool IsSolved
        {
            get
            {
                return true;
            }
        }

        public override object Answer
        {
            get
            {
                return 76576500;
            }
        }

    }
}
